package encrypt;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import com.example.sqlitecrypt.R;
import db.RandomGenerator;

import java.io.IOException;
import java.util.List;
/**
 * @author: wzq
 * @date: 14-6-17
 * description: change it at File | setting | File and code templates | include | file header
 */
public class ActQuote extends Activity {
    private Context mCtx;
    private int age = 0;
    String oldPwd = "oldPwd";
    String newPwd = "newPwd";
    byte[] stuff = {(byte)0xaa,(byte)0x55};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCtx = this;


        setContentView(R.layout.act_quote);

        DaoAbc.createTable(mCtx);
        findViewById(R.id.btn_insert).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = RandomGenerator.randomString(6);
                int age = RandomGenerator.randomInt(0, 99);
                // String pwd = RandomGenerator.randomString(8);
                String pwd = oldPwd;

                if(true) {
                    System.out.println("pwd = " + pwd);
                    String encPwd = AesTool.getInstance().encryptString(pwd);
                    System.out.println("encPwd = " + encPwd);
                    String decPwd = null;
                    try {
                        decPwd = AesTool.getInstance().decryptString(encPwd);
                    } catch(IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("decPwd = " + decPwd);
                }

                //

                BeanAbc aAbc = new BeanAbc(name, age, pwd,stuff);
                System.out.println("------before insert-----\n" + aAbc.toString());
                DaoAbc.insert(mCtx, aAbc);

                // check
                List<BeanAbc> list = DaoAbc.queryByName(mCtx, name);
                System.out.println("------query by name=" + name + "-----\n");
                if(list.size() == 0) {
                    System.out.println("query 结果为空");
                } else {
                    for(BeanAbc aa : list) {
                        System.out.println(aa.toString());
                    }
                }
            }
        });


        findViewById(R.id.btn_update).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DaoAbc.update(mCtx,oldPwd,newPwd);
            }
        });
    }
}
